package com.htl.线程.卖票;

public class TicketRunnable implements Runnable{
    //剩余球票
    private int count1 = 20;
    //已售出球票
    private int count2 = 0;

    @Override
    public void run() {
        while (count1 > 0) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (TicketRunnable.class){
                if (count1 == 0){
                    return;
                }
                count1--;
                count2++;
                if (count1 == 0) {
                    System.out.println(Thread.currentThread().getName()
                            + "售出第" + count2 +"张票，球票已经售完~");
                }else {
                    System.out.println(Thread.currentThread().getName()
                            + "售出第" + count2 + "张票，剩余" + count1 + "张票。");
                }
            }
        }
    }
}
